﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.ComponentModel;
using System.Data;
//using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Xml;

using Microsoft.SharePoint;

using FileMigrateConsole.Properties;

namespace FileMigrateConsole
{
    public static class CommonTool
    {
        public static char _SplitCharacter = '|';

        public static void writeDebugInfo(string strMessage)
        {
#if DEBUG
            sysWriteAppEntry("CommonTool", strMessage);
#endif
        }

        public static void writeDebugInfo(string strFilePrefix, string strMsg)
        {
#if DEBUG
            CommonTool.sysWriteAppEntry(strFilePrefix, strMsg, true);
#endif
        }

        public static void sysWriteAppEntry(string strMessage)
        {
            sysWriteAppEntry("CommonTool", strMessage);
            sysWriteAppEntry("CommonTool", ".....paused.......any key to continue......", true);
            Console.ReadLine();
        }

        public static void sysWriteAppEntry(string strFilePrefix, string strMessage)
        {
            sysWriteAppEntry(strFilePrefix, strMessage, true);
        }

        public static void sysWriteAppEntry(string strFilePrefix, string strMessage, bool boolConsoleFlag)
        {
            string MUTEX_FILE = @"FileMigrateConsole_Log_" + strFilePrefix;
            Mutex mutexFile = null;

            string strFileName = string.Empty;
            string strFolder = string.Empty;

            strFolder = Settings.Default.logpath; // @"c:\Temp";
            if (Directory.Exists(strFolder) == false)
            {
                strFolder = System.Environment.GetEnvironmentVariable(@"TEMP");
            }

            strFileName = strFilePrefix + @".Log." + DateTime.Today.ToString("yyyyMMdd") + @".txt";
            strFileName = strFolder + @"\" + strFileName;
            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    mutexFile = new Mutex(false, MUTEX_FILE);
                    mutexFile.WaitOne();  //Wait until the file is open

                    using (StreamWriter w = File.AppendText(strFileName))
                    {
                        string strTmp = "\r\n" + DateTime.Now.ToString("hh:mm:ss");

                        //w.WriteLine("SPSiteCurrentUser.LoginName=" + Convert.ToString(SPSiteCurrentUser.LoginName));
                        if (string.IsNullOrEmpty(strMessage) == false) strTmp += "\t" + strMessage;
                        w.WriteLine(strTmp);
                        if (boolConsoleFlag == true)
                            Console.WriteLine(strTmp);

                        w.Flush();
                        w.Close();
                    }
                }//);
            }
            catch (Exception ex)
            {
                throw new ApplicationException("CommonTool.cs - No rights to access log folder(" + strFolder + ")! ex.Message = " + ex.Message);
            }
            finally
            {
                if (mutexFile != null)
                {
                    mutexFile.ReleaseMutex();
                }
            }
        }
    }
}
